17. Return via Callback in MainActivity

Return via Callback in MainActivity

Before the quiz, we left off at the end of handler.postDelayed() in MessageDelayer.java, which runs a Runnable after a time delay.

handler.postDelayed() in MessageDelayer.java executes a Runnable after the amount of time set by DELAY_MILLIS

handler.postDelayed() in MessageDelayer.java executes a Runnable after the amount of time set by DELAY_MILLIS

Now, let’s look more closely into what happens when the callback is executed.

The Runnable executes a callback

The Runnable executes a callback

This method call indicates that we need go to the onDone() implemented in the callback activity, which in this case is MainActivity.

Going back to MainActivity.java in line 72 we find our implementation of onDone() in which we set the TextView to the text the user had typed in EditText

To round out the rest of MainActivity, there's the method getIdlingResource(), which has the annotations @VisibleForTesting and @NonNull, so it's only called during a test, and its value can’t be null.
The method returns a new instance of SimpleIdlingResource.

Summary

To summarize all the new classes and connections in this specific example:

  1. Implement the IdlingResource interface (SimpleIdlingResource.java)

  2. Create a callback interface (MessageDelayer.java) where the actual asynchronous task will occur

  3. Set the state of IdlingResource to false when the task is running, and then back to true when the task is done

  4. Have the delayer notify the activity that the process is complete via a callback (MainActivity.onDone)